import 'package:base_flutter/base_flutter.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/a_elder/models/elder_entity.dart';
import 'package:ls_work/a_elder/page/elder_detail_page.dart';
import 'package:ls_work/a_elder/page/elder_health_file_page.dart';
import 'package:ls_work/a_yyt/home_router.dart';
import 'package:ls_work/a_yyt/models/medical_elder_entity.dart';
import 'package:ls_work/a_yyt/page/Take_leave_home_page.dart';
import 'package:ls_work/a_yyt/page/health_home_page.dart';
import 'package:ls_work/a_yyt/page/home_care_report_page.dart';
import 'package:ls_work/a_yyt/page/home_life_edit_page.dart';
import 'package:ls_work/a_yyt/page/medical_check_page.dart';
import 'package:ls_work/a_yyt/widget/home_tag_view.dart';
import 'package:ls_work/config/permission_center_config.dart';
import 'package:ls_work/routers/fluro_navigator.dart';
import 'package:ls_work/ui/activity/activity_record_page.dart';
import 'package:ls_work/ui/leave/leave_record_page.dart';
import 'package:ls_work/ui/meal/meal_record_view_page.dart';
import 'package:ls_work/ui/medical/medical_record_page.dart';
import 'package:ls_work/util/app_navigator.dart';
import 'package:pc300_health_sdk/utils/health_device_link_utils.dart';
import 'package:permission_handler/permission_handler.dart';

import '../../a_elder/page/elder_case_list_page.dart';
import '../../ui/new_health/new_health_sign_page.dart';

class MyWorkView extends StatefulWidget {
  @override
  _MyWorkViewState createState() => _MyWorkViewState();

  final int? type;
  final String? elderNameLike;
  final int? customerId;
  final ElderInfo? info;

  MyWorkView(
      {Key? key, this.type, this.elderNameLike, this.customerId, this.info});
}

class _MyWorkViewState extends State<MyWorkView> {
  List<String> imgList = [];
  List<String> titleList = [];
  List<Function()> funcList = [];

  @override
  void initState() {
    ///0-1-2 - 3为首页
    ///4-5-6为长者信息界面
    if (widget.type == 0) {
      imgList = [
        'home/tag_2',
        'home/tag_10',
        'home/tag_3',
        'home/tag_17',
        'home/tag_18',
      ];
      titleList = ['待办事项', '零星收入', '长者生活', '体检包', '报警提醒'];
      funcList = [
        () {
          NavigatorUtils.push(context, HomeRouter.homePendingPage);
        },
        () {
          NavigatorUtils.push(context, HomeRouter.homeSporadicIncomePage);
        },
        () {
          NavigatorUtils.push(context, HomeRouter.homeOldLifePage);
        },
        () {
          Permission.bluetoothScan.checkRequestPermission(context,
              onSuccess: () {
            NavigatorUtils.push(context, HomeRouter.medicalPackagePage);
          }, msg: '连接体检包需要获取蓝牙权限');

          // Toast.show('开发中。。。');
        },
        () {
          NavigatorUtils.push(context, HomeRouter.medicalPolicyPage);
          // Toast.show('开发中。。。');
        }
      ];
    } else if (widget.type == 2) {
      // imgList = ['home/tag_4', 'home/tag_5', 'home/tag_6'];
      // titleList = ['个案/会办', '通讯录', '照护记录'];
      imgList = ['home/tag_5', 'home/tag_6', 'home/tag_9', 'home/tag_8'];
      titleList = ['通讯录', '照护记录', '请假记录', '健康登记'];
      funcList = [
        () {
          NavigatorUtils.push(context, HomeRouter.homeAddressBookPage);
        },
        () {
          NavigatorUtils.push(context, HomeRouter.homeCareReportPage);
        },
        () {
          AppNavigator.push(context, TakeLeaveHomePage());
        },
        () {
          AppNavigator.push(context, HealthHomePage());
        },
      ];
    }

    ///老人页面
    else if (widget.type == 4) {
      imgList = ['elder/tag1', 'elder/tag2', 'elder/dzda'];
      titleList = ['长者信息', '健康档案', '电子档案'];
      funcList = [
        () {
          AppNavigator.push(
              context,
              ElderDetailPage(
                info: widget.info!,
              ));
        },
        () {
          // NavigatorUtils.push(context, ElderRouter.elderHealthFilePage);
          AppNavigator.push(
              context,
              ElderHealthFilePage(
                customerId: widget.info!.id,
              ));
        },
        () {
          Go().push(ElderCaseListPage(
            customerElderId: widget.info?.id,
          ));
        }
      ];
    } else if (widget.type == 5) {
      imgList = [
        'home/tag_6',
        'icon_home_jkxx',
        'icon_home_hdxx',
        'icon_home_crxx',
        'icon_home_ysxx',
        'icon_home_jyxx',
        "home/tag_17"
      ];
      titleList = ['照护记录', '健康信息', '活动信息', '出入院信息', '饮食信息', '就医信息', '体检包'];
      funcList = [
        () {
          // NavigatorUtils.push(context, HomeRouter.homeSporadicIncomePage);
          AppNavigator.push(
              context,
              HomeCareReportPage(
                elderNameLike: widget.elderNameLike,
                customerId: widget.customerId,
                type: 1,
                info: widget.info,
              ));
        },
        () {
          Go().push(NewHealthSignPage(
            info: widget.info,
          ));
        },
        () {
          Go().push(ActivityRecordPage(
            info: widget.info!,
          ));
        },
        () {
          Go().push(LeaveRecordPage(
            info: widget.info!,
          ));
        },
        () {
          Go().push(MealRecordPage(
            info: widget.info!,
          ));
        },
        () {
          Go().push(MedicalRecordPage(
            info: widget.info!,
          ));
        },
        () {
          var data = widget.info;
          MedicalElderInfo info = new MedicalElderInfo(
            name: data?.name,
            id: data?.id,
            nurseLevelName: data?.nurseLevelName,
            roomNo: data?.roomNo.toString(),
            sexName: data?.sexName,
            age: data?.age,
          );
          Permission.bluetoothScan.checkRequestPermission(context,
              onSuccess: () {
            // Permission.locationWhenInUse.checkRequestPermission(context,
            //     onSuccess: () async {
              Permission.bluetoothConnect.checkRequestPermission(context,
                  onSuccess: () async {
                Go().push(
                  MedicalCheckPage(
                    info: info,
                    isOnConnectDevice: HealthDeviceLinkUtils.isOnline,
                  ),
                );
              }, msg: '连接体检包需要获取蓝牙连接权限');
            // }, msg: '连接健康设备需要获取定位权限');
          }, msg: '扫描体检包需要获取蓝牙权限');
        }
      ];
    } else if (widget.type == 6) {
      imgList = ['home/tag_3'];
      titleList = ['记录生活'];
      funcList = [
        () {
          AppNavigator.push(context, HomeLifeEditPage(info: widget.info));
        },
      ];
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // return Wrap(
    //   spacing: 16,
    //   runSpacing: 16,
    //   children: List.generate(titleList.length, (index){
    //     return HomeTagView(
    //       img: imgList[index],
    //       title: titleList[index],
    //       onTap: funcList[index],
    //     );
    //   }),
    // ).addToContainer(width: double.infinity);
    return GridView.builder(
      shrinkWrap: true,
      padding: EdgeInsets.symmetric(horizontal: 8, vertical: 0),
      physics: NeverScrollableScrollPhysics(),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3, childAspectRatio: 1),
      itemBuilder: (context, index) {
        return HomeTagView(
          img: imgList[index],
          title: titleList[index],
          onTap: funcList[index],
        );
      },
      itemCount: titleList.length,
    );
  }

  List<Widget> item() {
    List<Widget> temp = [];
    for (int i = 0; i < imgList.length; i++) {
      temp.add(Container(
        width: (widget.type == 0 || widget.type == 1)
            ? ScreenUtil.getScreenW(context) / 4.0
            : ScreenUtil.getScreenW(context) / 3.0,
        child: HomeTagView(
          img: imgList[i],
          title: titleList[i],
          onTap: funcList[i],
        ),
      ));
    }
    return temp;
  }
}
